﻿$(document).ready(function () {
    $("button").hide();
});

function searchGroup(input) {
    var keyWord = $(input).val();
    if (keyWord == "") {
        keyWord = "Search Group";
    }
    $.ajax({
        url: "/Group/Index?PageIndex=1&keyWord=" + keyWord + "&returnType=json",
        type: "POST",
        traditional: true,
        success: function (data, status, xhr) {
            var onSuccess = new OnSuccess(data);
            onSuccess.removeOldData();
            onSuccess.addDataToTables();
            onSuccess.createPageNavigator();
        },
        error: function (xhr, status, err) {
            alert("An error occus on sending information. Please reload this page")
        }
    });
}
$("#loading").ajaxStart(function () {
    $(this).center();
    $(this).show();
});

$("#loading").ajaxStop(function () {
    $(this).hide();
});

function OnSuccess(jsonObj) {
    this.Json = jsonObj;
    //remove data from table
    this.removeOldData = removeOldData;
    this.addDataToTables = addDataToTables;
    this.createPageNavigator = createPageNavigator;
}

function removeOldData() {
    $("tr[id]").remove();
}

function addDataToTables() {
    //add data to table

    if (this.Json.Groups.length > 0) {
        for (i = 0; i < this.Json.Groups.length; i++) {
            var row = "<tr id='"+ this.Json.Groups[i].GroupId +"'>";
			row += "<td><input  type='checkbox' name='GroupId' value='" + this.Json.Groups[i].GroupId + "'/></td>";
			row += "<td>" + this.Json.Groups[i].GroupName + "</td>";
			row += "<td>" + this.Json.Groups[i].NumberOfMember + "</td>";
			row += "<td>" + this.Json.Groups[i].NumberOfProject + "</td>";
            row += "<td>" + this.Json.Groups[i].NumberOfDefect + "</td>";
            row += "<td>" + this.Json.Groups[i].TotalAttachmentSize + "kb</td>";
            row += "<td>" + this.Json.Groups[i].CreatedDate + "</td>";
            row += "<td>" + this.Json.Groups[i].Creator + "</td>";
			row += "<td class='options-width'>";
			if (this.Json.Groups[i].IsActive) {
			    row += "<a href='/Group/EnableDisableGroup?id=" + this.Json.Groups[i].GroupId + "&act=Disable&pageIndex=" + this.Json.PageIndex + "&keyWord=" + this.Json.KeyWord + "' title='Disable this group' class='icon-close info-tooltip'></a>";
			}
			else {
			    row += "<a href='/Group/EnableDisableGroup?id=" + this.Json.Groups[i].GroupId + "&act=Enable&pageIndex=" + this.Json.PageIndex + "&keyWord=" + this.Json.KeyWord + "' title='Enable this group'class='icon-open info-tooltip'></a>";
			}
            row += "<a href='/Group/DeleteGroup?id=" + this.Json.Groups[i].GroupId + "&PageIndex=" + this.Json.PageIndex + " onclick=\"return confirm('Are you sure to delete this defect?')\" title='Delete' class='icon-2 info-tooltip'></a>";
			row += "</td>";
			row += "</tr>";
			$(".product-table").append(row);
        }
    }
}

function createPageNavigator() {
    //create projectmember page navigator
    var PageNavText = "";
    if (this.Json.PageIndex > 1) {
        if (this.Json.keyWord == "Search Group") {
            PageNavText += "<a class='page-far-left' href='/Group/Index/1'></a>";
            PageNavText += "<a class='page-left' href='/Group/Index/'" + this.Json.PageIndex - 1 +"' ></a>";
        } else {
            PageNavText += "<a class='page-far-left' href='/Group/Index/1/" + this.Json.KeyWord + "'></a>";
            PageNavText += "<a class='page-left' href='/Group/Index/" + this.Json.PageIndex - 1 + "/" + this.Json.KeyWord + "'></a>";
        }
    }
    PageNavText += "<div class='page-info>Page <strong>" + this.Json.PageIndex + "</strong>/ " + this.Json.TotalPages + "<div>'";
    if (this.Json.PageIndex < this.Json.TotalPages) {
        if (this.Json.keyWord == "Search Group") {
            PageNavText += "<a class='page-right' href='/Group/Index/" + this.Json.PageIndex + 1 + "'></a>";
            PageNavText += "<a class='page-far-right' href='/Group/Index/" + this.Json.TotalPages + "'></a>";
        } else {
            PageNavText += "<a class='page-right' href='/Group/Index/" + this.Json.PageIndex + 1 + "/" + this.Json.KeyWord + "'></a>";
            PageNavText += "<a class='page-far-right' href='/Group/Index/" + this.Json.TotalPages + "/" + this.Json.KeyWord + "'></a>";
        }
    }
    $("#PageNavigator").html(PageNavText);
}